<?php

/**
 * Sets the body-tag class attribute.
 *
 * Adds 'sidebar-left', 'sidebar-right' or 'sidebars' classes as needed.
 */
function phptemplate_body_class($sidebar_left, $sidebar_right) {
    if ($sidebar_left != '' && $sidebar_right != '') {
        $class = 'sidebars';
    }
    else {
        if ($sidebar_left != '') {
            $class = 'sidebar-left';
        }
        if ($sidebar_right != '') {
            $class = 'sidebar-right';
        }
    }

    if (isset($class)) {
        print ' class="'. $class .'"';
    }
}

/**
 * Return a themed breadcrumb trail.
 *
 * @param $breadcrumb
 *   An array containing the breadcrumb links.
 * @return a string containing the breadcrumb output.
 */
function phptemplate_breadcrumb($breadcrumb) {
    if (!empty($breadcrumb)) {
        return '<div class="breadcrumb">'. implode(' › ', $breadcrumb) .'</div>';
    }
}

/**
 * Allow themable wrapping of all comments.
 */
function phptemplate_comment_wrapper($content, $type = null) {
    static $node_type;
    if (isset($type)) $node_type = $type;

    if (!$content || $node_type == 'forum') {
        return '<div id="comments">'. $content . '</div>';
    }
    else {
        return '<div id="comments"><h2 class="comments">'. t('Comments') .'</h2>'. $content .'</div>';
    }
}

/**
 * Returns the rendered local tasks. The default implementation renders
 * them as tabs.
 *
 * @ingroup themeable
 */
function phptemplate_menu_local_tasks() {
    $output = '';

    if ($primary = menu_primary_local_tasks()) {
        $output .= "<ul class=\"tabs primary\">\n". $primary ."</ul>\n";
    }

    return $output;
}

/**
 * Add variables to the templates
 */
function _phptemplate_variables($hook, $vars = array()) {
    global $user;

    drupal_add_js(path_to_theme().'/dashboard_theme.js');

    switch ($hook) {
        case 'page':
            // Add the dashboard departments to the variables
            $dashboard_vid = variable_get('dashboard_vid', 0);
            $departments_tid = variable_get('dashboard_departments_tid', 0);

            $vars['departments'] = array_values(taxonomy_get_children($departments_tid, $dashboard_vid));

            $topics_tid = variable_get('dashboard_topics_tid', 0);

            $vars['topics'] = array_values(taxonomy_get_children($topics_tid, $dashboard_vid));

            if ($secondary = menu_secondary_local_tasks()) {
                $output = '<span class="clear"></span>';
                $output .= "<ul class=\"tabs secondary\">\n". $secondary ."</ul>\n";
                $vars['tabs2'] = $output;
            }

            // Hook into color.module
            if (module_exists('color')) {
                _color_page_alter($vars);
            }

            // Add user if not set
            if (!isset($vars['user'])) {
                $vars['user'] = $user;
            }

            break; // end 'page'

        case 'node':
            // Add the series term to the dashboard
            $series_vid = variable_get('series_vid', 0);

            // Find the series term
            $terms = taxonomy_node_get_terms_by_vocabulary($vars['nid'], $series_vid);
            if (count($terms) > 0) {
                $vars['series_term'] = array_pop($terms)->name;
            }

            // Add table sorter to grid dashboard
            if ($vars['type'] == 'dashboard_gv') {
                drupal_add_js(path_to_theme().'/jquery.tablesorter.js');
            }

            // Add table sorter to artwork listings
            $node = $vars['node'];

            // Add the icon term to the dashboard
            $icons_vid = variable_get('icons_vid', 0);

            // Find the icon term
            $terms = taxonomy_node_get_terms_by_vocabulary($vars['nid'], $icons_vid);
            foreach ($terms as $term) {
                if ($term->vid != $icons_vid) continue;

                // Now load the icon
                $query = "select n.nid from {term_node} tn inner join {node} n on tn.nid = n.nid " .
                    "where tn.tid = %d and n.type = 'image' and status = 1 limit 1";
                $node = db_fetch_object(db_query($query, $term->tid));

                if ($node) {
                    $node = node_load($node->nid);
                    $vars['icon_term'] = $term->name;
                    $vars['icon'] = $node;
                }
            }

            break; // end 'node'
    }

    return $vars;
}

/**
 * Override theme_views_bonus_panels_twocol_stacked
 */
function phptemplate_views_bonus_panels_twocol_stacked($view, $nodes, $type) {
    $content = array();
    foreach ($nodes as $count => $n) {
        $node = node_load($n->nid);
        if ($count == 0) {
            $content['top'] .= node_view($node, false, false, true);
        }
        else if ($count % 2) {
            $content['left'] .= node_view($node, true, false, false);
        }
        else {
            $content['right'] .= node_view($node, true, false, false);
        }
    }
    return panels_print_layout('twocol_stacked', $content);
}
